package com.huatec.hiot_cloud.core.controller;

import com.huatec.hiot_cloud.core.config.Result;
import com.huatec.hiot_cloud.core.config.ResultStatus;
import com.huatec.hiot_cloud.core.entity.Device;
import com.huatec.hiot_cloud.core.service.IDeviceService;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("device")
@Api(value = "device", description = "设备管理")
public class DeviceController {
    @Resource
    private IDeviceService deviceService;

    @RequestMapping(value = "getdevicelist", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取设备列表", notes = "获取设备列表:id,title,dev_type,mac,status,user_id,created,updated,deviceimg,template_id,description")
    public Result getDeviceList(@RequestBody(required = false) @ModelAttribute Device device){
        List<Device> redata = deviceService.getDeviceList(device);
        return Result.ok(ResultStatus.SELECT_SUCCESS,redata);
    }

    @RequestMapping(value = "adddevice", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "新增设备", notes = "新增设备:title,dev_type,mac,status,user_id,created,updated,deviceimg,template_id,description")
    public Result addDevice(@RequestBody(required = false) @ModelAttribute Device device){
        int redata = deviceService.addDevice(device);
        if (redata == 1){
            return Result.ok(ResultStatus.SAVE_SUCCESS);
        }

        return Result.error(ResultStatus.SAVE_ERROR);
    }

    @RequestMapping(value = "deldevice", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "删除设备", notes = "删除设备:id")
    public Result delDevice(@RequestBody(required = false) @ModelAttribute Device device){
        if (device.getId() == null){
            return Result.error(ResultStatus.PARM_ERROR);
        }
        int redata = deviceService.delDeviceById(device);
        if (redata == 1){
            return Result.ok(ResultStatus.DELETE_SUCCESS);
        }

        return Result.error(ResultStatus.DELETE_ERROR);
    }

    @RequestMapping(value = "updatedevice", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "修改设备", notes = "修改设备:id,title,dev_type,mac,status,user_id,created,updated,deviceimg,template_id,description")
    public Result updateDevice(@RequestBody(required = false) @ModelAttribute Device device){
        if (device.getId() == null){
            return Result.error(ResultStatus.PARM_ERROR);
        }
        int redata = deviceService.updateDeviceById(device);
        if (redata >= 1){
            return Result.ok(ResultStatus.UPDATE_SUCCESS);
        }

        return Result.error(ResultStatus.UPDATE_ERROR);
    }
}
